package service

import (
	"context"
	"errors"

	"octopus/user/dto"
	"octopus/user/infra/acl/convert"
	"octopus/user/infra/query"
	"octopus/user/infra/repository"
	"octopus/user/model/domain/user"
)

type UserService struct {
	repo  user.Repository
	ctx   context.Context
	store *query.Store
}

func NewUserService(ctx context.Context) *UserService {
	us := &UserService{
		repo: repository.NewUserRepoImpl(ctx),
		ctx:  ctx,
	}
	us.store = query.NewTransactionStore(ctx, us.repo.DB())
	return us
}

// Create create a user.
func (u *UserService) Create(args *dto.CreateUserRequest) error {
	exist, err := u.store.UserExist(args.Email, args.Mobile)
	if err != nil {
		return err
	}
	if exist {
		return errors.New("user already exist")
	}
	// convert user to user.User
	userDomain := convert.CreateUserDTO2User(u.ctx, args)
	if !userDomain.Valid() {
		return errors.New("user info is invalid")
	}
	return u.repo.Create(userDomain)
}

func (u *UserService) Login(args *dto.LoginRequest) error {
	// check user exist
	// user domain
	// login domain
	return nil
}
